# 字典类型
import json
from datetime import datetime

from sqlalchemy import func
from sqlalchemy.orm import aliased

from apps.model.baseModel import BaseModel
from apps.settingApi.api.lessees.model import lesseesModel
from comm.sqlalchemyConfig import db


class printOrderModel(db.Model):
    """
    打印单据
    """
    __tablename__ = 'tx_print_order'
    id = db.Column(db.String(100), primary_key=True, autoincrement=False,doc='id')
    code = db.Column(db.String(100), nullable=False,unique=True,doc='code',comment="单据的code唯一标识")
    name = db.Column(db.String(125), nullable=False,doc='单据名称')
    @classmethod
    def getAll(cls):
        # 获取所有数据
        dataList = []
        data = cls.query.filter().all()
        for itm in data:
            itm_z = {}
            itm_z['id'] = itm.id
            itm_z['code'] = itm.code
            itm_z['name'] = itm.name
            dataList.append(itm_z)
        return dataList

class printTemplateModel(db.Model):
    """
    打印模板
    """
    __tablename__ = 'tx_print_template'
    id = db.Column(db.String(100), primary_key=True, autoincrement=False,doc='id')
    print_order_code = db.Column(db.String(125), nullable=False,doc='code',comment="归属打印单据来源表tx_print_order",)
    name = db.Column(db.String(125), nullable=False,doc='打印模板名称')
    lessees_id = db.Column(db.String(125), nullable=False, doc='归属租户')
    template = db.Column(db.Text, nullable=False,doc='打印模板对象字符串')
    sort = db.Column(db.Integer, nullable=True,default=1000,doc='排序')
    create_time = db.Column(db.DateTime, default=datetime.now, nullable=True)
    update_time = db.Column(db.DateTime, default=datetime.now, onupdate=datetime.now, nullable=True)

    @classmethod
    def getAll(cls,print_order_code=None,lessees_id=None,name=None):
        # 获取所有数据
        dataList = []
        filterList = []
        if print_order_code!=None and print_order_code!='':
            filterList.append(cls.print_order_code==print_order_code)
        if lessees_id!=None and lessees_id!='':
            filterList.append(cls.lessees_id==lessees_id)
        if name!=None and name!='':
            filterList.append(cls.name.like('%' + str(name) + '%'))
        filterList.append(cls.print_order_code == printOrderModel.code)
        filterList.append(cls.lessees_id == lesseesModel.id)
        data = db.session.query(cls,printOrderModel,lesseesModel).filter(*filterList).all()
        for itm1,itm2,itm3 in data:
            itm_z = {}
            itm_z['id'] = itm1.id
            itm_z['print_order_code'] = itm1.print_order_code
            itm_z['print_order_name'] = itm2.name
            itm_z['name'] = itm1.name
            itm_z['lessees_id'] = itm1.lessees_id
            itm_z['lessees_name'] = itm3.name
            # itm_z['template'] = itm1.template
            itm_z['create_time'] = itm1.create_time
            itm_z['update_time'] = itm1.update_time
            itm_z['sort'] = itm1.sort
            print(itm_z)
            dataList.append(itm_z)
        return dataList

    @classmethod
    def getPk(cls, pk):

        result = db.session.query(cls, printOrderModel, lesseesModel).filter(cls.id==pk).first()
        if result == None:
            return None,'没有查询到数据'
        itm1,itm2,itm3 = result
        itm_z = {}
        itm_z['id'] = itm1.id
        itm_z['print_order_code'] = itm1.print_order_code
        itm_z['print_order_name'] = itm2.name
        itm_z['name'] = itm1.name
        itm_z['lessees_id'] = itm1.lessees_id
        itm_z['lessees_name'] = itm3.name
        itm_z['template'] = json.loads(itm1.template)
        itm_z['create_time'] = itm1.create_time
        itm_z['update_time'] = itm1.update_time
        itm_z['sort'] = itm1.sort

        return itm_z,None


class printTaskNameModel(db.Model):
    """
    打印任务名称
    """
    __tablename__ = 'tx_print_task_name'
    id = db.Column(db.String(100), primary_key=True, autoincrement=False,doc='id')
    name = db.Column(db.String(125), nullable=False,doc='任务名称')
    lessees_id = db.Column(db.String(125), nullable=False, doc='归属租户')
    @classmethod
    def getAll(cls):
        # 获取所有数据
        dataList = []
        data = cls.query.filter().all()
        for itm in data:
            itm_z = {}
            itm_z['id'] = itm.id
            itm_z['name'] = itm.name
            itm_z['lessees_id'] = itm.lessees_id
            dataList.append(itm_z)
        return dataList
class printTaskListModel(db.Model):
    """
    打印任务
    """
    __tablename__ = 'tx_print_task_list'
    id = db.Column(db.String(100), primary_key=True, autoincrement=False,doc='id')
    print_template_id = db.Column(db.String(125), nullable=False,doc='归属哪个打印模板',comment='来源表tx_print_template')
    task_id = db.Column(db.String(125), nullable=False,doc='打印任务名称')
    lessees_id = db.Column(db.String(125), nullable=False,doc='归属租户')
    qty = db.Column(db.Integer, nullable=True,default=1,doc='打印数量')
    status = db.Column(db.Boolean, nullable=True, default=1, doc='状态',comment="是否打印完成1，未完成，0完成")
    create_time = db.Column(db.DateTime, default=datetime.now, nullable=True)
    update_time = db.Column(db.DateTime, default=datetime.now, onupdate=datetime.now, nullable=True)
